<template>
    <form-container title="编辑车间信息" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
        <a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
            <a-form-item label="车间编码：" name="workshopCode">
                <a-input v-model:value="formState.workshopCode" placeholder="请输入主题" allow-clear />
            </a-form-item>
        </a-form>
        <a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
            <a-form-item label="车间名称：" name="workshopName">
                <a-input v-model:value="formState.workshopName" placeholder="请输入主题" allow-clear />
            </a-form-item>
        </a-form>
        <a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
            <a-form-item label="描述：" name="workshopDesc">
                <a-input v-model:value="formState.workshopDesc" placeholder="请输入主题" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
        </template>
    </form-container>
</template>

<script setup name="updatePassword">
    import { required } from '@/utils/formRules'
    import workshopApi from '@/api/mes/basicdata/workshopApi'

    // 定义emit事件
    const emit = defineEmits({ successful: null })
    // 默认是关闭状态
    let visible = ref(false)
    const formRef = ref()
    // 表单数据
    const formState = ref({})
    const submitLoading = ref(false)

    // 打开抽屉
    const onOpen = (email) => {
        visible.value = true
        formState.value = email
    }
    // 关闭抽屉
    const onClose = () => {
        visible.value = false
    }

    // 提交数据
    const onSubmit = async () => {
        formRef.value.validateFields().then(() => {
            submitLoading.value = true
            let formData = {
                id: formState.value.id,
                workshopCode: formState.value.workshopCode,
                workshopName: formState.value.workshopName,
                workshopDesc: formState.value.workshopDesc,
                updateTime: formState.value.updateTime
            }
            // 提交数据
            workshopApi
                .workshopUpdate(formData)
                .then(() => {
                    //formRef.value.resetFields()
                    visible.value = false
                    emit('successful', formData)
                    
                })
                .finally(() => {
                    submitLoading.value = false
                    callback() //调用组件中的回调方法
                })
        })
            .catch(() => {
                console.error('表单验证失败')
        })
    }
    // 调用这个函数将子组件的一些数据和方法暴露出去
    defineExpose({
        onOpen
    })
</script>

<style scoped></style>